<template>
  <van-address-edit
    :area-list="areaList"
    show-postal
    show-delete
    show-set-default
    show-search-result
    :search-result="searchResult"
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    @delete="onDelete"
    @change-detail="onChangeDetail"
  />
</template>
<script>
import { Toast } from "vant";
import * as address from "@/api/address.js";
import areaList from "@/utils/area.js";

import { mapGetters } from "vuex";

export default {
  data() {
    return {
      areaList,
      searchResult: [],
      provinceList: [],
      cityList: [],
      countyList: [],
      provinceId: "",
      cityId: "",
      areaId: ""
    };
  },

  computed: {
    ...mapGetters(["userId"])
  },

  methods: {
    getProvince() {
      //省
      address.addressProvince().then(res => {
        // eslint-disable-next-line
        console.log(res);
        this.provinceList = res;
      });
    },
    getCity() {
      //市
      address
        .addressCity({
          province_id: 1
        })
        .then(res => {
          this.cityList = res;
        });
    },
    getArea() {
      //区、县
      address
        .addressArea({
          city_id: this.cityId
        })
        .then(res => {
          this.countyList = res;
        });
    },
    onSave(res) {
      address
        .addressAdd({
          user_id: this.userId,
          phone: res.tel,
          area_id: res.areaCode,
          street1: res.province + res.city + res.county,
          street2: res.addressDetail,
          is_default: res.isDefault ? 1 : 2,
          full_name: res.name
        })
        .then(() => {
          this.$router.go(-1);
        });
    },
    onDelete() {
      Toast("delete");
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区"
          }
        ];
      } else {
        this.searchResult = [];
      }
    }
  },
  created() {
    this.getProvince();
    this.getCity();
  }
};
</script>